package com.example.homelicaisystem.control;

import com.example.homelicaisystem.entity.*;

import com.example.homelicaisystem.entity.DTO.ExpendDetailDTO;
import com.example.homelicaisystem.entity.exception.UsedCashException;
import com.example.homelicaisystem.entity.exception.WrongEnoughMoney;
import com.example.homelicaisystem.services.CardService;
import com.example.homelicaisystem.services.ExpendDetailService;
import com.example.homelicaisystem.services.UserService;
import com.example.homelicaisystem.utils.HandleMoney;
import com.example.homelicaisystem.utils.Result;
import com.example.homelicaisystem.utils.UserUtil;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/expendDetail")
@RestController
@Slf4j
public class ExpendDetailControl {
    private final ExpendDetailService expendDetailService;
    private final UserService userService;
    private final CardService cardService;

    public ExpendDetailControl(ExpendDetailService expendDetailService, UserService userService, CardService cardService) {
        this.expendDetailService = expendDetailService;
        this.userService = userService;
        this.cardService = cardService;
    }

    private static int BANK = 3;

    @PostMapping("/list")
    public Result<ListWithCount<ExpendDetail>> getList(@RequestBody ExpendDetailDTO data) {
        log.debug("收到请求list请求。参数:{}", data);



        User user = userService.getUserById(UserUtil.getUserId());
        data.setFamilyId(user.getFamilyId());

        Page<ExpendDetail> res = expendDetailService.getList(data);

        //数据库中金额单位为 分  前台单位为  元
        res.forEach(expendDetail -> {
            expendDetail.setMoney(HandleMoney.showMoney(expendDetail.getMoney()));
        });
        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }

    @DeleteMapping("/delete/{id}")
//    没有返回值，泛型用？
    public Result<?> delete(@PathVariable int id) {
        log.debug("收到delete请求。");
        ExpendDetail data = expendDetailService.getExpendDetailById(id);
        if (data.getModeId() == BANK) {
            cardService.increaseBank(data);
        } else {
            userService.increaseCash(data);
        }

        expendDetailService.delete(id);
        return Result.ok(null);
    }

    @PatchMapping("/modify")
    public Result<?> modify(@RequestBody ExpendDetail data) {
        log.debug("收到modify请求。参数:{}", data);

        ExpendDetail expendDetail = expendDetailService.getExpendDetailById(data.getId());
        // 记录新旧收入记录
        int oldMoney = expendDetail.getMoney();
        int dataMoney = data.getMoney();
        int changeMoney = 0;

        if (dataMoney >= oldMoney) {
            changeMoney = dataMoney - oldMoney;
            data.setMoney(changeMoney);
            if (data.getModeId() == BANK) {
                cardService.decreaseBank(data);
            } else {
                userService.decreaseCash(data);
            }
        } else {
            changeMoney = oldMoney - dataMoney;
            data.setMoney(changeMoney);
            if (data.getModeId() == BANK) {
                cardService.increaseBank(data);
            } else {
                userService.increaseCash(data);
            }
        }
        data.setMoney(dataMoney);
        expendDetailService.modify(data);
        return Result.ok(null);
    }

    @PostMapping("/create")
    public Result<Integer> create(@RequestBody ExpendDetail data) {
        log.debug("收到create请求。参数:{}", data);
        //支出的金额改成 分 为单位
        data.setMoney(HandleMoney.backendMoney(data.getMoney()));

        if (data.getModeId() == BANK) {
            try {
                cardService.decreaseBank(data);
            } catch (UsedCashException e) {
                throw new WrongEnoughMoney("金额不足");
            }
        } else {
            try {
                userService.decreaseCash(data);
                //默认业务银行卡号为1
                data.setCardId(1);
            } catch (UsedCashException e) {
                throw new WrongEnoughMoney("金额不足");
            }
        }

        expendDetailService.create(data);
        return Result.ok(data.getId());
    }

    @PostMapping("/selectExpendType")
    public Result<List<SelectResult>> selectExpendType() {
        log.debug("收到selectIncomeType请求");
        return Result.ok(expendDetailService.selectExpendType());
    }

    @PostMapping("/selectExpendMode")
    public Result<List<SelectResult>> selectExpendMode() {
        log.debug("收到selectIncomeMode请求");
        return Result.ok(expendDetailService.selectExpendMode());
    }
}
